package com.mxpio.erp.common.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtil {

    // 定义日期格式
    public static DateTimeFormatter YMD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter DEFAULT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter MD = DateTimeFormatter.ofPattern("MM-dd");

    // 定义日期格式
   public static SimpleDateFormat DATE_YMD = new SimpleDateFormat("yyyy-MM-dd");

    public static long getDaysBetween(Date startDate, Date endDate) {
        try {
            Instant startInstant = startDate.toInstant();
            Instant endInstant = endDate.toInstant();

            // Convert to LocalDate
            LocalDate startLocalDate = startInstant.atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate endLocalDate = endInstant.atZone(ZoneId.systemDefault()).toLocalDate();

            return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getNow(DateTimeFormatter formatter) {
        try {
            // 获取当前日期
            LocalDateTime now = LocalDateTime.now();


            // 格式化当前日期
            return now.format(formatter);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String formatDate(Date reportTime, SimpleDateFormat format) {
        try {
            // 格式化当前日期
            return format.format(reportTime);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date str2Date(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, DEFAULT);

        // 转成 java.util.Date
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
